import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {NavService} from "../../services/nav/nav.service";
import {Nav} from "../../models/nav";

@Component({
  selector: 'app-nav-bar',
  templateUrl: './nav-bar.component.html',
  styleUrls: ['./nav-bar.component.less']
})
export class NavBarComponent implements OnInit {

  navItems: Array<Nav>;

  @Output('indexContents')
  indexContents: EventEmitter<number> = new EventEmitter<number>();

  oldIndex: number = 0;

  constructor(private navService: NavService) {
  }

  ngOnInit() {
    setTimeout(() => {
      this.navItems = this.navService.openNavs[this.navService.index].navItems;
      this.navItems.forEach((value, index) => {
        if(index == 0){
          value.isActive = true;
        }else {
          value.isActive = false;
        }
      })
    }, 100);
  }

  /*向父组件发送消息，用来改变内容区域的内容*/
  sendContent(i: number) {
    this.navItems[i].isActive = true;
    this.navItems[i].isShowChildren = false;
    if (i != this.oldIndex) {
      this.navItems[this.oldIndex].isActive = false;
    }
    this.oldIndex = i;
    this.indexContents.emit(i);
  }

  mouseOver(i) {
    this.navItems[i].isShowChildren = true;
  }

  mouseMove(i) {
    this.navItems[i].isShowChildren = false;
  }

}
